package smartbooks.ui.orgnanization;

import smartbooks.ui.users.UsersPanel;
import java.awt.Container;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import smartbooks.exceptions.MyException;
import smartbooks.ui.listeners.CommonMouseListener;
import smartbooks.restrictDocuments.JTextFieldMaxLimitDocument;
import smartbooks.ui.SmartBooksUserSettings;
import smartbooks.ui.mainframe.MainFrame;
import smartbooks.ui.manager.OrganizationManager;
import smartbooks.ui.util.CommonConstants;
import static smartbooks.ui.util.CommonConstants.CREATE_BUTTON_TEXT;
import static smartbooks.ui.util.CommonConstants.CUSTOMER_PANEL_CREATE_TEXT;
import static smartbooks.ui.util.CommonConstants.CUSTOMER_PANEL_EDIT_TEXT;
import static smartbooks.ui.util.CommonConstants.SAVE_BUTTON_TEXT;
import smartbooks.ui.util.CommonFunctions;
import smartbooksdb.exception.MandatoryFieldException;
import smartbooksdb.hibernatebean.Organization;

/**
 *
 * @author Ankit
 */
public class OrganizationPanel extends javax.swing.JPanel implements CommonConstants {
    private Organization editOrganization = null;
    Organization organization = new Organization();
    boolean defaultOrgImageIcon = true;
    
    /**
     * Creates new form OrganizationPanel
     * @param editOrganization
     */
    public OrganizationPanel(Organization editOrganization) {
        if(null!=editOrganization) {
            this.editOrganization = editOrganization;
            this.organization = editOrganization;
        }
        
        initComponents();
        
        customInitComponents();
    }
    
    /**
     * This method is used to get Organization
     * @return organization
     */
    public Organization getOrganization() {
        return this.organization;
    }
    
    /**
     * This method is used to set Organization
     * @param organization
     */
    public void setOrganization(Organization organization) {
        this.organization = organization;
    }
    
    private void customInitComponents() {
        addKeyListenersToComponents(keyListener);
        
        if(organization.getOrgId()==0) {
            organizationPanel.setBorder(new TitledBorder(organizationPanel.getBorder(), CUSTOMER_PANEL_CREATE_TEXT, 2, 0, new Font("Default", 0, 14)));
            createOrSaveOrgButton.setText(CREATE_BUTTON_TEXT);
        } else {
            organizationPanel.setBorder(new TitledBorder(organizationPanel.getBorder(), CUSTOMER_PANEL_EDIT_TEXT, 2, 0, new Font("Default", 0, 14)));
            createOrSaveOrgButton.setText(SAVE_BUTTON_TEXT);
        }
        
        editOrgImageLabel.setVisible(false);
        deleteOrgImageLabel.setVisible(false);
    }
 
    private void assignAdditionalOrganizationValues(String organizationModificationType) {
        try {
            if(!defaultOrgImageIcon) {
                organization.setPhoto(CommonFunctions.getBLOBImageFromIcon(orgImageLabel.getIcon()));
            }
            
            if(organizationModificationType.equals(CREATE_BUTTON_TEXT)) {
                organization.setCreatedBy(SmartBooksUserSettings.USERNAME);
            }
            organization.setUpdatedBy(SmartBooksUserSettings.USERNAME);
        } catch (SQLException | IOException ex) {
            Logger.getLogger(UsersPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void addKeyListenersToComponents(KeyListener keyListener) {
        nameTextField.addKeyListener(keyListener);
        regNoTextField.addKeyListener(keyListener);
        stateComboBox.addKeyListener(keyListener);
        cityComboBox.addKeyListener(keyListener);
        emailTextField.addKeyListener(keyListener);
        phoneTextField.addKeyListener(keyListener);
        mobileTextField.addKeyListener(keyListener);
        officeTextField.addKeyListener(keyListener);
        faxTextField.addKeyListener(keyListener);
    }
    
    private void clearOrganizationPanel() {
        nameTextField.setText("");
        regNoTextField.setText("");
        descriptionTextArea.setText("");
        addressTextArea.setText("");
        stateComboBox.setSelectedIndex(0);
        cityComboBox.setSelectedIndex(0);
        emailTextField.setText("");
        phoneTextField.setText("");
        mobileTextField.setText("");
        officeTextField.setText("");
        faxTextField.setText("");
        internalNotesTextArea.setText("");
    }
    
    private void setModifiedOrganizationInParent() {
        Container c = this;
        
        while(c.getParent()!=null) {
            c = c.getParent();
            
            if(c instanceof OrganizationDialog) {
                ((OrganizationDialog)c).setModifiedOrganization(organization);
            }
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        orgImageFileChooser = new javax.swing.JFileChooser();
        organizationPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        regNoTextField = new javax.swing.JTextField();
        orgImagePanel = new javax.swing.JPanel();
        editOrgImageLabel = new javax.swing.JLabel();
        deleteOrgImageLabel = new javax.swing.JLabel();
        orgImageLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionTextArea = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        addressTextArea = new javax.swing.JTextArea();
        stateComboBox = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        cityComboBox = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        emailTextField = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        phoneTextField = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        mobileTextField = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        officeTextField = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        faxTextField = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        internalNotesTextArea = new javax.swing.JTextArea();
        jSeparator1 = new javax.swing.JSeparator();
        createOrSaveOrgButton = new javax.swing.JButton();
        clearOrgButton = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();

        orgImageFileChooser.setAcceptAllFileFilterUsed(false);
        orgImageFileChooser.setFileFilter(new FileNameExtensionFilter("images", "jpg","jpeg","png"));

        organizationPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " ", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("<html>Name:<font color=red>*</font></html>");

        nameTextField.setDocument(new JTextFieldMaxLimitDocument(64));
        nameTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${organization.orgName}"), nameTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "orgNameBinding");
        bindingGroup.addBinding(binding);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Registered No:");

        regNoTextField.setDocument(new JTextFieldMaxLimitDocument(32));
        regNoTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${organization.regNum}"), regNoTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "regNumBinding");
        bindingGroup.addBinding(binding);

        orgImagePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        orgImagePanel.setPreferredSize(new java.awt.Dimension(100, 100));
        orgImagePanel.setRequestFocusEnabled(false);
        orgImagePanel.setLayout(null);

        editOrgImageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smartbooks/images/edit_16-16.png"))); // NOI18N
        editOrgImageLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        editOrgImageLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                editOrgImageLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                editOrgImageLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                editOrgImageLabelMouseExited(evt);
            }
        });
        orgImagePanel.add(editOrgImageLabel);
        editOrgImageLabel.setBounds(0, 0, 16, 16);

        deleteOrgImageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smartbooks/images/delete_16-16.png"))); // NOI18N
        deleteOrgImageLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        deleteOrgImageLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                deleteOrgImageLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                deleteOrgImageLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                deleteOrgImageLabelMouseExited(evt);
            }
        });
        orgImagePanel.add(deleteOrgImageLabel);
        deleteOrgImageLabel.setBounds(80, 0, 16, 16);

        orgImageLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        orgImageLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        orgImageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smartbooks/images/organization-default.png"))); // NOI18N
        orgImageLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                orgImageLabelMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                orgImageLabelMouseEntered(evt);
            }
        });
        orgImagePanel.add(orgImageLabel);
        orgImageLabel.setBounds(0, 0, 100, 100);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Description:");

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        descriptionTextArea.setColumns(20);
        descriptionTextArea.setDocument(new JTextFieldMaxLimitDocument(256));
        descriptionTextArea.setLineWrap(true);
        descriptionTextArea.setRows(4);
        descriptionTextArea.setWrapStyleWord(true);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${organization.description}"), descriptionTextArea, org.jdesktop.beansbinding.BeanProperty.create("text"), "descriptionBinding");
        bindingGroup.addBinding(binding);

        descriptionTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                descriptionTextAreaKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(descriptionTextArea);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("Address:");

        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        addressTextArea.setColumns(20);
        addressTextArea.setDocument(new JTextFieldMaxLimitDocument(256));
        addressTextArea.setLineWrap(true);
        addressTextArea.setRows(4);
        addressTextArea.setWrapStyleWord(true);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${organization.address}"), addressTextArea, org.jdesktop.beansbinding.BeanProperty.create("text"), "addressBinding");
        bindingGroup.addBinding(binding);

        addressTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                addressTextAreaKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(addressTextArea);

        stateComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        stateComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${organization.state}"), stateComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "stateBinding");
        bindingGroup.addBinding(binding);

        jLabel9.setText("State");
        jLabel9.setEnabled(false);

        cityComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        cityComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${organization.city}"), cityComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "cityBinding");
        bindingGroup.addBinding(binding);

        jLabel8.setText("City");
        jLabel8.setEnabled(false);

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel13.setText("Email:");

        emailTextField.setDocument(new JTextFieldMaxLimitDocument(64));
        emailTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${organization.email}"), emailTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "emailBinding");
        bindingGroup.addBinding(binding);

        jLabel14.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel14.setText("Phone:");

        phoneTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        phoneTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${organization.phone}"), phoneTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "phoneBinding");
        bindingGroup.addBinding(binding);

        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel15.setText("Mobile:");

        mobileTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        mobileTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${organization.mobile}"), mobileTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "mobileBinding");
        bindingGroup.addBinding(binding);

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel16.setText("Office:");

        officeTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        officeTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${organization.office}"), officeTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "officeBinding");
        bindingGroup.addBinding(binding);

        jLabel17.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel17.setText("Fax:");

        faxTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        faxTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${organization.fax}"), faxTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "faxBinding");
        bindingGroup.addBinding(binding);

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel18.setText("Internal Notes:");

        jScrollPane3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        internalNotesTextArea.setColumns(20);
        internalNotesTextArea.setLineWrap(true);
        internalNotesTextArea.setRows(3);
        internalNotesTextArea.setWrapStyleWord(true);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${organization.notes}"), internalNotesTextArea, org.jdesktop.beansbinding.BeanProperty.create("text"), "notesBinding");
        bindingGroup.addBinding(binding);

        internalNotesTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                internalNotesTextAreaKeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(internalNotesTextArea);

        createOrSaveOrgButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        createOrSaveOrgButton.setText("Create");
        createOrSaveOrgButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createOrSaveOrgButtonActionPerformed(evt);
            }
        });

        clearOrgButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        clearOrgButton.setText("Clear");
        clearOrgButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearOrgButtonActionPerformed(evt);
            }
        });

        jLabel10.setText("<html><font color=red>*</font></html>");

        jLabel11.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel11.setText("Required Field");

        javax.swing.GroupLayout organizationPanelLayout = new javax.swing.GroupLayout(organizationPanel);
        organizationPanel.setLayout(organizationPanelLayout);
        organizationPanelLayout.setHorizontalGroup(
            organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(organizationPanelLayout.createSequentialGroup()
                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(organizationPanelLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(organizationPanelLayout.createSequentialGroup()
                                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                                    .addComponent(regNoTextField))
                                .addGap(18, 18, 18)
                                .addComponent(orgImagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(organizationPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(organizationPanelLayout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(clearOrgButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(createOrSaveOrgButton))
                            .addComponent(jSeparator1)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, organizationPanelLayout.createSequentialGroup()
                                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel13)
                                    .addComponent(jLabel14)
                                    .addComponent(jLabel16)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jScrollPane2)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, organizationPanelLayout.createSequentialGroup()
                                        .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(organizationPanelLayout.createSequentialGroup()
                                                .addComponent(jLabel9)
                                                .addGap(0, 0, Short.MAX_VALUE))
                                            .addComponent(stateComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(cityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel8)))
                                    .addComponent(emailTextField)
                                    .addGroup(organizationPanelLayout.createSequentialGroup()
                                        .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(officeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(phoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                                        .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel15)
                                            .addComponent(jLabel17))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(faxTextField)
                                            .addComponent(mobileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addComponent(jLabel18, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        organizationPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {faxTextField, mobileTextField, officeTextField, phoneTextField});

        organizationPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {clearOrgButton, createOrSaveOrgButton});

        organizationPanelLayout.setVerticalGroup(
            organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, organizationPanelLayout.createSequentialGroup()
                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(organizationPanelLayout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel2)
                            .addComponent(regNoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(organizationPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(orgImagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(emailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel14)
                    .addComponent(phoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15)
                    .addComponent(mobileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel16)
                    .addComponent(officeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17)
                    .addComponent(faxTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel18)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(organizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel11)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(clearOrgButton)
                    .addComponent(createOrSaveOrgButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(organizationPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(organizationPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void descriptionTextAreaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_descriptionTextAreaKeyPressed
        CommonFunctions.textboxTABPolicy(evt);
    }//GEN-LAST:event_descriptionTextAreaKeyPressed

    private void addressTextAreaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_addressTextAreaKeyPressed
        CommonFunctions.textboxTABPolicy(evt);
    }//GEN-LAST:event_addressTextAreaKeyPressed

    private void internalNotesTextAreaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_internalNotesTextAreaKeyPressed
        CommonFunctions.textboxTABPolicy(evt);
    }//GEN-LAST:event_internalNotesTextAreaKeyPressed

    private void editOrgImageLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_editOrgImageLabelMouseClicked
        try {
            int status = orgImageFileChooser.showOpenDialog(this);
            if (status == JFileChooser.APPROVE_OPTION) {
                File f = orgImageFileChooser.getSelectedFile();
                String filename = f.getAbsolutePath();
                ImageIcon ii = new ImageIcon(CommonFunctions.scaleImage((int) orgImagePanel.getPreferredSize().getWidth(), (int) orgImagePanel.getPreferredSize().getHeight(), ImageIO.read(new File(filename))));
                orgImageLabel.setIcon(ii);
                defaultOrgImageIcon = false;
            }
        } catch (Exception ex) {
            Logger.getLogger(UsersPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_editOrgImageLabelMouseClicked

    private void editOrgImageLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_editOrgImageLabelMouseEntered
        editOrgImageLabel.setVisible(true);
        if (!defaultOrgImageIcon) {
            deleteOrgImageLabel.setVisible(true);
        }
    }//GEN-LAST:event_editOrgImageLabelMouseEntered

    private void editOrgImageLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_editOrgImageLabelMouseExited
        editOrgImageLabel.setVisible(false);
        deleteOrgImageLabel.setVisible(false);
    }//GEN-LAST:event_editOrgImageLabelMouseExited

    private void deleteOrgImageLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteOrgImageLabelMouseClicked
        orgImageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smartbooks/images/organization-default.png")));
        deleteOrgImageLabel.setVisible(false);
        defaultOrgImageIcon = true;
    }//GEN-LAST:event_deleteOrgImageLabelMouseClicked

    private void deleteOrgImageLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteOrgImageLabelMouseEntered
        editOrgImageLabel.setVisible(true);
        if (!defaultOrgImageIcon) {
            deleteOrgImageLabel.setVisible(true);
        }
    }//GEN-LAST:event_deleteOrgImageLabelMouseEntered

    private void deleteOrgImageLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteOrgImageLabelMouseExited
        editOrgImageLabel.setVisible(false);
        deleteOrgImageLabel.setVisible(false);
    }//GEN-LAST:event_deleteOrgImageLabelMouseExited

    private void orgImageLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_orgImageLabelMouseEntered
        editOrgImageLabel.setVisible(true);
        if (!defaultOrgImageIcon) {
            deleteOrgImageLabel.setVisible(true);
        }
    }//GEN-LAST:event_orgImageLabelMouseEntered

    private void orgImageLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_orgImageLabelMouseExited
        editOrgImageLabel.setVisible(false);
        deleteOrgImageLabel.setVisible(false);
    }//GEN-LAST:event_orgImageLabelMouseExited

    private void clearOrgButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearOrgButtonActionPerformed
        clearOrganizationPanel();
    }//GEN-LAST:event_clearOrgButtonActionPerformed

    private void createOrSaveOrgButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createOrSaveOrgButtonActionPerformed
        CommonMouseListener.hideException = false;
        
        try {
            boolean displayMainFrame = (SmartBooksUserSettings.DEFAULT_ORG_ID==0);
            
            assignAdditionalOrganizationValues(createOrSaveOrgButton.getText());
            
            boolean organizationModified = false;
            switch (createOrSaveOrgButton.getText()) {
                case CREATE_BUTTON_TEXT:
                    organizationModified = OrganizationManager.createOrganization(organization);
                    break;
                case SAVE_BUTTON_TEXT:
                    organizationModified = OrganizationManager.updateOrganization(organization, editOrganization);
                    break;
            }
            
            if(organizationModified) {
                Toolkit.getDefaultToolkit().beep();
                
                switch (createOrSaveOrgButton.getText()) {
                    case CREATE_BUTTON_TEXT:
                        JOptionPane.showMessageDialog(this,"Organization is created successfully !","SmartBooks - Success",JOptionPane.INFORMATION_MESSAGE);
                        break;
                    case SAVE_BUTTON_TEXT:
                        JOptionPane.showMessageDialog(this,"Organization is updated successfully !","SmartBooks - Success",JOptionPane.INFORMATION_MESSAGE);
                        break;
                }
                
                setModifiedOrganizationInParent();
                CommonFunctions.dispose(this);
                
                if(displayMainFrame) {
                    MainFrame mainFrame = new MainFrame();
                }
            } else {
                Toolkit.getDefaultToolkit().beep();
                JOptionPane.showMessageDialog(this,"There was some error in creating Organization.\nKindly contact system administrator.","SmartBooks - Error",JOptionPane.ERROR_MESSAGE);
            }
        } catch (MyException me) {
            if(me.getComponentText().equalsIgnoreCase(ORGANIZATION_NAME)) {
                me.showBalloonTip(nameTextField);
                nameTextField.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(STATE)) {
                me.showBalloonTip(stateComboBox);
                stateComboBox.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(CITY)) {
                me.showBalloonTip(cityComboBox);
                cityComboBox.requestFocus();
            }
        } catch (MandatoryFieldException ex) {
            Logger.getLogger(OrganizationPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_createOrSaveOrgButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea addressTextArea;
    private javax.swing.JComboBox cityComboBox;
    private javax.swing.JButton clearOrgButton;
    private javax.swing.JButton createOrSaveOrgButton;
    private javax.swing.JLabel deleteOrgImageLabel;
    private javax.swing.JTextArea descriptionTextArea;
    private javax.swing.JLabel editOrgImageLabel;
    private javax.swing.JTextField emailTextField;
    private javax.swing.JTextField faxTextField;
    private javax.swing.JTextArea internalNotesTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField mobileTextField;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JTextField officeTextField;
    private javax.swing.JFileChooser orgImageFileChooser;
    private javax.swing.JLabel orgImageLabel;
    private javax.swing.JPanel orgImagePanel;
    private javax.swing.JPanel organizationPanel;
    private javax.swing.JTextField phoneTextField;
    private javax.swing.JTextField regNoTextField;
    private javax.swing.JComboBox stateComboBox;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    
    KeyListener keyListener = new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {}

        @Override
        public void keyPressed(KeyEvent e) {}

        @Override
        public void keyReleased(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER) { 
                createOrSaveOrgButtonActionPerformed(null);
            }
        }
    };
}
